using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;

namespace Xms.Abp.Emailing;

public class BackgroundEmailSendingJob : AsyncBackgroundJob<EmailMessage>, ITransientDependency
{
    protected IEmailSender EmailSender { get; }
    protected ICurrentTenant CurrentTenant { get; }

    public BackgroundEmailSendingJob(
        IEmailSender emailSender,
        ICurrentTenant currentTenant)
    {
        EmailSender = emailSender;
        CurrentTenant = currentTenant;
    }

    public async override Task ExecuteAsync(EmailMessage args)
    {
        using (CurrentTenant.Change(args.TenantId))
        {
            await EmailSender.SendAsync(args);
        }
    }
}
